package com.example.front.main.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.utils.MyAdapter;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PsGameActivity extends AppCompatActivity {
    private String URL;
    private MyApplication myApplication;
    private ListView psgame_listView;
    private MyAdapter<PsGame> psGameMyAdapter = null;
    private List<PsGame> psGameList = null;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_psgame);
        myApplication = (MyApplication)getApplication();
        URL = myApplication.getURL();
        psgame_listView = findViewById(R.id.psgame_listView);
        OkHttpUtils.get()
                .url(URL + "/psgame/getPsgameList")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Toast.makeText(PsGameActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onResponse(String response) {
                        String json = response;
                        psGameList = new ArrayList<PsGame>();
                        try {
                            JSONArray jsonArray = new JSONArray(json);
                            for (int i = 0; i < jsonArray.length(); i++){
                                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                PsGame psGame = new PsGame();
                                psGame.setId(i+1);
                                psGame.setName(jsonObject.getString("name"));
                                psGame.setIntroduction(jsonObject.getString("introduction"));
                                psGame.setLanguage(jsonObject.getString("yuyan"));
                                psGame.setGameType(jsonObject.getString("type"));
                                psGame.setScore(jsonObject.getDouble("score"));
                                psGame.setImageUrl(jsonObject.getString("imageUrl"));
                                psGameList.add(psGame);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        psGameMyAdapter = new MyAdapter<PsGame>((ArrayList) psGameList, R.layout.psgame_list_item){
                            @Override
                            public void bindView(ViewHolder holder, PsGame obj){
                                imageView = holder.getView(R.id.psgame_image);
                                Glide.with(PsGameActivity.this)
                                        .load(obj.getImageUrl())
                                        .error(R.drawable.error)
                                        .crossFade()
                                        .into(imageView);
                                holder.setText(R.id.id_textView, obj.getId() + "");
                                holder.setText(R.id.name_textView, obj.getName());
                                holder.setText(R.id.type_textView, obj.getGameType() + " " + obj.getLanguage());
                                holder.setText(R.id.score_textView, obj.getScore() + "");
                            }
                        };
                        psgame_listView.setAdapter(psGameMyAdapter);
                        psgame_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                PsGame psGame = psGameList.get(position);
                                Intent intent = new Intent(PsGameActivity.this, PsGameIntroductionActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString("name", psGame.getName());
                                bundle.putDouble("score", psGame.getScore());
                                bundle.putString("type", psGame.getGameType());
                                bundle.putString("yuyan", psGame.getLanguage());
                                bundle.putString("introduction", psGame.getIntroduction());
                                bundle.putString("imageUrl", psGame.getImageUrl());
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });
                    }
                });
    }
}
